package com.neborosoft.FlyingDog.android;

import android.app.Activity;
import android.app.Application;
import android.content.ContentResolver;
import com.neborosoft.FlyingDog.android.database.AndroidAudioDataBase;
import com.neborosoft.FlyingDog.android.playing.Player;
import com.neborosoft.FlyingDog.android.playing.PlayingModeManager;
import com.neborosoft.FlyingDog.bitmap.ImageLoaderConfigFactory;
import com.neborosoft.FlyingDog.logic.database.AudioDataManager;
import com.neborosoft.FlyingDog.logic.database.LocalAudioDataBase;
import com.neborosoft.android.events.EventManager;
import com.neborosoft.android.events.HistoryManager;
import com.neborosoft.android.gui.search.SearchDialog;
import com.neborosoft.android.threading.AndroidOnHostThreadExecutor;
import com.neborosoft.lib.stream.TextLoader;
import com.neborosoft.lib.stream.TextLoaderConfig;
import com.neborosoft.lib.threading.OnHostThreadExecutor;
import com.neborosoft.lib.threading.Threading;
import com.neborosoft.lib.utilities.Utilities;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class FlyingDog extends Application{
    private static final int BACK_BUTTON_HISTORY_STACK_SIZE = 100;

    private static FlyingDog instance;

    private OnHostThreadExecutor onHostThreadExecutor;
    private Executor audioDataManagerExecutor;
    private Executor songsYouMayLikeExecutor;
    private Player player;

    class ExceptionHandler implements Thread.UncaughtExceptionHandler{
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            Utilities.logException(throwable);
        }
    }

    public static FlyingDog getInstance(){
        return instance;
    }

    public void onActivityCreated(Activity activity) {
        player.onActivityCreated(activity);
    }

    public void onActivityDestroyed(Activity activity){
        player.onActivityDestroyed();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            instance = this;

            ExceptionHandler exceptionHandler = new ExceptionHandler();
            Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);

            TextLoaderConfig textLoaderConfig = TextLoaderConfigFactory.getCommonConfig(getBaseContext());
            TextLoader.init(textLoaderConfig);

            audioDataManagerExecutor = Threading.newExecutorWithSingleThreadAndSingleTaskQueue();
            songsYouMayLikeExecutor = Threading.newExecutorWithSingleThreadAndSingleTaskQueue();

            onHostThreadExecutor = new AndroidOnHostThreadExecutor();
            ContentResolver contentProvider = getContentResolver();
            LocalAudioDataBase localAudioDataBase = new AndroidAudioDataBase(contentProvider, audioDataManagerExecutor);
            AudioDataManager.init(onHostThreadExecutor, audioDataManagerExecutor, songsYouMayLikeExecutor,
                    localAudioDataBase);

            ImageLoaderConfiguration configuration
                    = ImageLoaderConfigFactory.getCommonImageLoaderConfig(getApplicationContext());
            ImageLoader.getInstance().init(configuration);

            HistoryManager.init(BACK_BUTTON_HISTORY_STACK_SIZE);

            EventManager.init(onHostThreadExecutor);

            player = new Player(getApplicationContext());

        } catch (Exception e) {
            Utilities.logException(e);
        }
    }
}
